package com.stp.houbb;

import com.github.houbb.heaven.util.lang.CharUtil;
import com.github.houbb.sensitive.word.api.ISensitiveWordReplace;
import com.github.houbb.sensitive.word.api.ISensitiveWordReplaceContext;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义替换器
 * SensitiveWordHelper.replace(“五星红旗迎风飘扬，毛主席的画像屹立在天安门前”, new CustomSensitiveWordReplace());
 * 打印结果：旗帜迎风飘扬，教员的画像屹立在门前
 */
public class CustomSensitiveWordReplace implements ISensitiveWordReplace {
    @Override
    public String replace(ISensitiveWordReplaceContext context) {
        String sensitiveWord = context.sensitiveWord();

        //TODO 可以在项目初始化敏感词字典 这里引用
        Map<String, String> wordMap = initSensitiveWordMap();
        if(wordMap.containsKey(sensitiveWord)){
            return wordMap.get(sensitiveWord);
        }

        // 其他默认使用 * 代替
        int wordLength = context.wordLength();
        return CharUtil.repeat('*', wordLength);
    }

    private Map<String,String> initSensitiveWordMap(){
        Map<String,String> map = new HashMap<String,String>(){{
            put("五星红旗","旗帜");
            put("天安门","门");
            put("毛主席","教员");
            put("叼","*");
        }};
        return map;
    }
}
